package ex_from_web;



	import java.text.SimpleDateFormat;
	import java.util.Calendar;
	/**
	 * Creating a Java thread using Thread class example
	 * Easiest way to create a Java thread is by extending
	 * Thread class and call start() method on thread object
	 */
	class Clock extends Thread {
	    Calendar calendar;
	    long time;      //in milliseconds
	    long startTime; //in milliseconds
	    SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss");
	    String name;
	    
	    public Clock(String name) {
	        this.name = name;
	        calendar  = Calendar.getInstance();
	        time      = calendar.getTimeInMillis();
	        startTime = calendar.getTimeInMillis();
	    }
	    
	    public void run() {
	        while(true) {
	            long currentTime = System.currentTimeMillis();
	            //when 1s has been passed, print the time
	            if (currentTime - time >= 1000) {
	                time += 1000;
	                System.out.println(name + " time: " + df.format(startTime)+ " <-> " + df.format(currentTime));
	            }
	            //turn off clock after 5s
	            if (currentTime - startTime > 18000) {
	                break;
	            }
	        }
	    }
	}

	public class threads_again {

	    public static void main(String[] args) {
	            Clock clock1 = new Clock("Clock 1");
	            //Start the first clock
	            clock1.start();
	            //Make a 3s pause before start the second clock
	            try {
	                Thread.sleep(2000);
	            } catch (InterruptedException e) {
	                System.out.println("Interrupted!");
	            }
	            //Start the second clock
	            Clock clock2 = new Clock("Clock 2");
	            clock2.start();
	          //Make a 3s pause before start the second clock
	            try {
	                Thread.sleep(2000);
	            } catch (InterruptedException e) {
	                System.out.println("Interrupted!");
	            }
	            //Start the second clock
	            Clock clock3 = new Clock("Clock 3");
	            clock3.start();
	        }
	}